package com.tenbent.product.center.insures.action;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.insures.bo.SafeguardCategory;
import com.tenbent.product.center.insures.dto.SafeguardCategoryDto;
import com.tenbent.product.center.insures.dto.search.SafeguardCategoryPageDto;
import com.tenbent.product.center.insures.service.SafeguardCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 保障项目小分类
 *
 * @author Randy
 * 
 *         Created by ThinkPad on 2017/8/28.
 */
@RestController
public class SafeguardCategoryController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SafeguardCategoryController.class);

	public static class Url {
		/** 添加产品 */
		public static final String save = "/api/safeguardCategory/save";
		/** 修改产品 */
		public static final String update = "/api/safeguardCategory/update";
		/** 删除产品 */
		public static final String delete = "/api/safeguardCategory/delete/{id}";
		/** 根据产品ID查询 */
		public static final String queryByTypeCode = "/api/safeguardCategory/query_by_type_code";
		/** 分页查询 */
		public static final String query = "/api/safeguardCategory/query";
	}

	@Autowired
	@Qualifier("safeguardCategoryServiceImpl")
	private SafeguardCategoryService safeguardCategoryService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	public SafeguardCategoryDto saveApi(@Valid @RequestBody SafeguardCategoryDto safeguardCategoryDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		settingSaveManager(safeguardCategoryDto, managerId);
		SafeguardCategory safeguardCategory = safeguardCategoryDto.transToBo();
		safeguardCategoryService.add(safeguardCategory);
		return new SafeguardCategoryDto(safeguardCategory);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public SafeguardCategoryDto updateApi(@Valid @RequestBody SafeguardCategoryDto safeguardCategoryDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		if (safeguardCategoryDto.getId() == null) {
			errors.rejectValue("id", "safeguard.category.id.not.empty",
					getMessage("safeguard.category.id.not.empty", null));
		}
		processErrors(errors);
		settingModiferManager(safeguardCategoryDto, managerId);
		SafeguardCategory safeguardCategory = safeguardCategoryDto.transToBo();
		safeguardCategoryService.update(safeguardCategory);
		return new SafeguardCategoryDto(safeguardCategory);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void deleteApi(@PathVariable Integer id, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		safeguardCategoryService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<SafeguardCategoryDto> query(SafeguardCategoryPageDto searchPageDto) {

		PageResult<SafeguardCategory> pageResult = safeguardCategoryService
				.queryPageObj(searchPageDto.transToPageable());
		return new Page(change.changesToDTOPage(pageResult, SafeguardCategoryDto.class));
	}

	@RequestMapping(value = Url.queryByTypeCode, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public List<SafeguardCategoryDto> queryByProductId(@RequestParam("typeCode") String typeCode) {

		SafeguardCategory safeguardCategory = new SafeguardCategory();
		safeguardCategory.setTypeCode(typeCode);
		List<SafeguardCategory> list = safeguardCategoryService.queryObj(safeguardCategory);
		return change.changesToDTOList(list, SafeguardCategoryDto.class);
	}
}
